/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.modules.gis.rest;

import cn.hutool.core.date.LocalDateTimeUtil;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.modules.gis.domain.FeedHistory;
import me.zhengjie.modules.gis.service.FeedHistoryService;
import me.zhengjie.modules.gis.service.dto.FeedHistoryDto;
import me.zhengjie.modules.gis.service.dto.FeedHistoryQueryCriteria;
import me.zhengjie.utils.DateUtil;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @author ZhuangBing Yan
* @date 2022-08-15
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "进料运输历史管理")
@RequestMapping("/api/feedHistory")
public class FeedHistoryController {

    private final FeedHistoryService feedHistoryService;

    @AnonymousAccess
    @GetMapping("/findArea")
    @Log("查看辖区排放量")
    @ApiOperation("查看辖区排放量")
    @ApiImplicitParams({@ApiImplicitParam(name = "enterInTime",value ="查询时间月份",paramType ="query",dataType="String",
            defaultValue= "2022-07")})
    public ResponseEntity<Object> findArea( @RequestParam(defaultValue ="2022-07",value = "enterInTime") String enterInTime){
        return new ResponseEntity<>(feedHistoryService.findArea(enterInTime),HttpStatus.OK);
    }


    @AnonymousAccess
    @GetMapping("/findStartPoint")
    @Log("查看排放点分布")
    @ApiOperation("查看排放点分布")
    @ApiImplicitParams({@ApiImplicitParam(name = "enterInTime",value ="查询时间月份",paramType ="query",dataType="String",
            defaultValue= "2022-07")})
    public ResponseEntity<Object> findStartPoint( @RequestParam(defaultValue ="2022-07",value = "enterInTime") String enterInTime){
        return new ResponseEntity<>(feedHistoryService.findStartPoint(enterInTime),HttpStatus.OK);
    }


    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('feedHistory:list')")
    public void exportFeedHistory(HttpServletResponse response, FeedHistoryQueryCriteria criteria) throws IOException {
        feedHistoryService.download(feedHistoryService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询进料运输历史")
    @ApiOperation("查询进料运输历史")
    @PreAuthorize("@el.check('feedHistory:list')")
    public ResponseEntity<Object> queryFeedHistory(FeedHistoryQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(feedHistoryService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @GetMapping("/getByDate")
    @Log("查询进料运输历史")
    @ApiOperation("查询进料运输历史")
    @ApiImplicitParams({@ApiImplicitParam(name = "enterInTime",value ="查询时间月份",paramType ="query",dataType="String",
            defaultValue= "2022-07")})
    public ResponseEntity<Object> getByDate( @RequestParam(defaultValue ="2022-07",value = "enterInTime") String enterInTime, Pageable pageable){
        return new ResponseEntity<>(feedHistoryService.getByDate(enterInTime,pageable),HttpStatus.OK);
    }

    @GetMapping("/getLineDataByDate")
    @Log("查询进料运输历史")
    @ApiOperation("查询进料运输历史")
    @ApiImplicitParams({@ApiImplicitParam(name = "enterInTime",value ="查询时间月份",paramType ="query",dataType="String",
            defaultValue= "2022-07")})
    public ResponseEntity<Object> getLineDataByDate( @RequestParam(defaultValue ="2022-07",value = "enterInTime") String enterInTime, Pageable pageable){
        return new ResponseEntity<>(feedHistoryService.getLineDataByDate(enterInTime,pageable),HttpStatus.OK);
    }

    @GetMapping("getDuringTime")
    @Log("查询时间线的历史进料数据")
    @ApiOperation("查询时间线的历史数据")
    public ResponseEntity<FeedHistoryDto> getDuringTime(Map<String,String> params){
        return new ResponseEntity<FeedHistoryDto>(feedHistoryService.findById(1L),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增进料运输历史")
    @ApiOperation("新增进料运输历史")
    @PreAuthorize("@el.check('feedHistory:add')")
    public ResponseEntity<Object> createFeedHistory(@Validated @RequestBody FeedHistory resources){
        return new ResponseEntity<>(feedHistoryService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改进料运输历史")
    @ApiOperation("修改进料运输历史")
    @PreAuthorize("@el.check('feedHistory:edit')")
    public ResponseEntity<Object> updateFeedHistory(@Validated @RequestBody FeedHistory resources){
        feedHistoryService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除进料运输历史")
    @ApiOperation("删除进料运输历史")
    @PreAuthorize("@el.check('feedHistory:del')")
    public ResponseEntity<Object> deleteFeedHistory(@RequestBody Long[] ids) {
        feedHistoryService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}